/**
 * 
 */
package com.music.common.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.farng.mp3.TagException;
import org.springframework.dao.DataAccessException;

import com.music.common.dao.IMediaDetails;
import com.music.common.entity.Media;
import com.music.common.errorhandler.DaoException;
import com.music.common.errorhandler.MemusicException;
import com.music.common.presentation.forms.MediaForm;
import com.music.common.service.IMediaService;
import com.music.common.service.ServiceFinder;
import com.music.content.persistnce.MemusicFileUtil;

/**
 * @author rpatil
 * 
 */
public class MediaService implements IMediaService {

	public MediaService() {
	}

	/**
	 * @param userName
	 * @param password
	 * @return
	 * @throws MemusicException
	 * @throws DataAccessException
	 * @throws java.sql.SQLException
	 */
	public List getAllMediaDetailByUserName(String userName,
			HttpServletRequest request) throws MemusicException {
		IMediaDetails mediaDetails = (IMediaDetails) ServiceFinder.getContext(
				request).getBean("MediaHibernateDao");
		try {
			return mediaDetails.getAllMediaDetailByUserName(userName);
		} catch (DaoException e) {
			throw new MemusicException(e);
		}
	};

	public Media getMediaById(Integer mediaId) throws IOException, TagException {
		try {
			MemusicFileUtil fileUtil = new MemusicFileUtil();
			Media media = fileUtil.getFile();
			return media;
		} catch (IOException ex) {
			throw new IOException(ex);
		}
	}
	
	public List<Media> getMediaListByDirectory(String directory, HttpServletRequest request) throws IOException, TagException, DaoException{
		MemusicFileUtil fileUtil = new MemusicFileUtil();
		IMediaDetails mediaDetails = (IMediaDetails) ServiceFinder.getContext(request).getBean("MediaHibernateDao");
		Integer counter = mediaDetails.getMediaCounter();
		return (List<Media>)fileUtil.getFilesFromDirectory(directory, counter);
	}
	
	public Media getMediaDetailFromFile(File file, HttpServletRequest request) throws IOException, TagException, DaoException{
		MemusicFileUtil fileUtil = new MemusicFileUtil();
		IMediaDetails mediaDetails = (IMediaDetails) ServiceFinder.getContext(request).getBean("MediaHibernateDao");
		Integer counter = mediaDetails.getMediaCounter();
		return fileUtil.getMediaDetailFromFile(file, counter);
	}
	
	public Integer getMediaCounter(HttpServletRequest request) throws DaoException{
		IMediaDetails mediaDetails = (IMediaDetails) ServiceFinder.getContext(request).getBean("MediaHibernateDao");
		return mediaDetails.getMediaCounter();
	}
	
	public Media updateMedia(MediaForm mediaForm, HttpServletRequest request) throws DaoException{
		Media media = new Media();
		media.setTitle(mediaForm.getTitle());
		media.setAlbum(mediaForm.getAlbum());
		media.setArtist(mediaForm.getArtist());
		media.setCategory(mediaForm.getCategory());
		media.setComments(mediaForm.getComments());
		media.setComposer(mediaForm.getComposer());
		media.setDirector(media.getDirector());
		media.setEqualizer(mediaForm.getEqualizer());
		media.setFileId(mediaForm.getFileId());
		media.setFileLocation(mediaForm.getFileLocation());
		media.setGenreId(mediaForm.getGenreId());
		media.setGrouping(mediaForm.getGrouping());
		media.setLinks(mediaForm.getLinks());
		media.setLyricist(mediaForm.getLyricist());
		media.setLyrics(mediaForm.getLyrics());
		media.setMediaId(mediaForm.getMediaId());
		media.setPlayCount(0);
		media.setProducer(mediaForm.getProducer());
		media.setRating(mediaForm.getRating());
		media.setTags(mediaForm.getTags());
		media.setTrackNo(mediaForm.getTrackNo());
		media.setUpdatedBy(mediaForm.getUpdatedBy());
		media.setAddedBy(mediaForm.getAddedBy());
		media.setYear(mediaForm.getYear());
		IMediaDetails mediaDetails = (IMediaDetails) ServiceFinder.getContext(request).getBean("MediaHibernateDao");
		mediaDetails.save(media);
		return media;
	}
}
